package cn.fintecher.pangolin.service.business.task;

import cn.fintecher.pangolin.common.enums.ApprovalStage;
import cn.fintecher.pangolin.common.enums.AssistStatus;
import cn.fintecher.pangolin.entity.mysql.AssistCase;
import cn.fintecher.pangolin.entity.mysql.ProcessApplication;
import cn.fintecher.pangolin.entity.mysql.QAssistCase;
import cn.fintecher.pangolin.entity.mysql.QProcessApplication;
import cn.fintecher.pangolin.service.business.model.response.CheckCaseResponse;
import cn.fintecher.pangolin.service.business.model.response.ProcessApplicationResponse;
import cn.fintecher.pangolin.service.business.repository.jparepository.AssistCaseRepository;
import cn.fintecher.pangolin.service.business.repository.jparepository.ProcessApplicationRepository;
import org.apache.commons.collections4.IterableUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 14:18 2020/1/8
 */

@Service("checkCaseTask")
public class CheckCaseTask {
    @Autowired
    private AssistCaseRepository assistCaseRepository;

    @Autowired
    private ProcessApplicationRepository processApplicationRepository;

    /**
     * 检查案件是否存在协催申请和协催案件
     */
    @Async
    public CompletableFuture<CheckCaseResponse> doTask(List<String> caseIds) {
        CheckCaseResponse response = new CheckCaseResponse();
        List<String> caseNumbers1 = new ArrayList<>();
        List<String> caseNumbers2 = new ArrayList<>();
        Iterable<ProcessApplication> applicationIterable = processApplicationRepository.findAll(QProcessApplication.processApplication.caseId.in(caseIds)
                .and(QProcessApplication.processApplication.approvalStage.eq(ApprovalStage.WAIT_APPROVAL)
                        .or(QProcessApplication.processApplication.approvalStage.eq(ApprovalStage.BEING_APPROVAL))));
        List<ProcessApplication> processApplications = IterableUtils.toList(applicationIterable);
        for (ProcessApplication processApplication : processApplications) {
            if (!caseNumbers1.contains(processApplication.getCaseNumber())) {
                caseNumbers1.add(processApplication.getCaseNumber());
            }
        }

        Iterable<AssistCase> iterable = assistCaseRepository.findAll(QAssistCase.assistCase.caseId.in(caseIds)
                .and(QAssistCase.assistCase.assistStatus.in(AssistStatus.ASSIST_WAIT_ASSIGN, AssistStatus.ASSIST_COLLECTING)));
        List<AssistCase> assistCaseList = IterableUtils.toList(iterable);
        for (AssistCase assistCase : assistCaseList) {
            if (!caseNumbers2.contains(assistCase.getCaseNumber())) {
                caseNumbers2.add(assistCase.getCaseNumber());
            }
        }
        response.setCaseNumbers1(caseNumbers1);
        response.setCaseNumbers2(caseNumbers2);
        return CompletableFuture.completedFuture(response);
    }
}
